package com.pospi.pai.yunpos.more;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.view.listener.OnCreateBodyViewListener;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.dao.GoodsDao;
import com.pospi.dto.GoodsDto;
import com.pospi.dto.ScalesBeen;
import com.pospi.greendao.ScalesBeenDao;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.been.SupBeen;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.pai.yunpos.util.BillUtil;
import com.pospi.pai.yunpos.util.SocketUtil;
import com.pospi.util.App;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by acer on 2018/9/7.
 */

public class ScaleMangerActivity extends Base2Activity {
    @Bind(R.id.list_goods)
    ListView list_goods;
    private MyAdapter adapter;
    private ScalesBeenDao dao;
    private EditText et_num;
    private EditText et_name;
    private EditText et_ip;
    private EditText et_xh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_sacles_manger);
        ButterKnife.bind(this);
        setTitle("电子秤管理");
        setMenuImage(R.drawable.menu_add);
        dao = App.getInstance().getDaoSession().getScalesBeenDao();
        List<ScalesBeen> beens = dao.queryBuilder().list();
        dtos.addAll(beens);
        init();
    }

    private void init() {
        adapter = new MyAdapter();
        list_goods.setAdapter(adapter);
        list_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showdzc(dtos.get(position));
            }
        });
       list_goods.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
               new AlertDialog.Builder(ScaleMangerActivity.this)
                       .setTitle("提示")
                       .setMessage("是否删除？")
                       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                               dao.delete(dtos.get(position));
                               dtos.remove(dtos.get(position));
                               adapter.notifyDataSetChanged();
                           }
                       }).setNegativeButton("取消", null).show();

               return true;
           }
       });


    }

    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
        showdzc(null);
    }


    private List<ScalesBeen> dtos = new ArrayList<>();
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dtos.size();
        }

        @Override
        public Object getItem(int i) {
            return dtos.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(ScaleMangerActivity.this).inflate(R.layout.scale_bill_item, null);
                holder.name = view.findViewById(R.id.name);
                holder.tv_dj = view.findViewById(R.id.tv_dj);
                holder.tv_dw = view.findViewById(R.id.tv_dw);
                holder.tv_xh = view.findViewById(R.id.tv_xh);
                holder.code = view.findViewById(R.id.code);
                holder.tv_plu = view.findViewById(R.id.tv_plu);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            ScalesBeen bill = dtos.get(i);
            holder.name.setText(bill.getNum());
            holder.code.setText(bill.getName());
            holder.tv_dj.setText(bill.getIp());
            holder.tv_dw.setText(bill.getXh());
            holder.tv_xh.setText(String.valueOf(i + 1));
            holder.tv_plu.setVisibility(View.GONE);
            return view;
        }

        class Holder {
            TextView name;
            TextView tv_dj;
            TextView tv_dw;
            TextView code;
            TextView tv_xh;
            TextView tv_plu;

        }
    }
    private void showdzc(final ScalesBeen scalesBeen) {
        String title = "新增";
        if (scalesBeen != null) {
            title = scalesBeen.getName();
        }
         new CircleDialog.Builder(this)
                .setBodyView(R.layout.dialog_dzc_gl, new OnCreateBodyViewListener() {
                    @Override
                    public void onCreateBodyView(View view) {
                        et_num = view.findViewById(R.id.et_num);
                        et_name = view.findViewById(R.id.et_name);
                        et_ip = view.findViewById(R.id.et_ip);
                        et_xh = view.findViewById(R.id.et_xh);
                        if (scalesBeen != null) {
                            et_num.setText(scalesBeen.getNum());
                            et_name.setText(scalesBeen.getName());
                            et_ip.setText(scalesBeen.getIp());
                            et_xh.setText(scalesBeen.getXh());
                        }

                    }
                })
                .setWidth(0.5f)
                .setPositive("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (scalesBeen != null) {
                            scalesBeen.setName(et_name.getText().toString().trim());
                            scalesBeen.setIp(et_ip.getText().toString().trim());
                            scalesBeen.setNum(et_num.getText().toString().trim());
                            scalesBeen.setXh(et_xh.getText().toString().trim());
                            dao.update(scalesBeen);
                        } else {
                            ScalesBeen been = new ScalesBeen();
                            been.setName(et_name.getText().toString().trim());
                            been.setIp(et_ip.getText().toString().trim());
                            been.setNum(et_num.getText().toString().trim());
                            been.setXh(et_xh.getText().toString().trim());
                            dao.insert(been);
                            dtos.add(been);
                        }
                        adapter.notifyDataSetChanged();

                    }
                })
                .setCancelable(false)
                .setTitle(title)
                .setNegative("取消", null)
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        hideSoftInput();
                    }
                })
                .show();
    }


}
